﻿using System;
using System.Web;

namespace SlamCms.Web
{
	public class DefaultBaseUriResolver : IBaseUriResolver
	{
		private HttpContextBase httpContextBase;

		public DefaultBaseUriResolver(HttpContextBase httpContextBase)
		{
			this.httpContextBase = httpContextBase;
		}

		public Uri GetBaseUri()
		{
			var currentUri = httpContextBase.Request.Url;
			if (currentUri == null)
				return null;

			Uri uri;
			if (Uri.TryCreate(string.Format("{0}://{1}", currentUri.Scheme, currentUri.Authority), UriKind.Absolute, out uri))
				return uri;

			return null;
		}
	}
}